﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Top.Api.Util;
using System.IO;
using Top.Api.Response;
using Top.Api.Domain;
using WangMiEntity.Download;
using TaoBaoServices;

namespace DownloadTaobaoAPI
{
    public class Util
    {
        public static void DownloadAndProcess(string url)
        {
            string zipFile = AtsUtils.Download(url, "d:/Downloads/Zip");
            List<string> files = AtsUtils.Unzip(zipFile, "d:/Downloads/Unzip");
            foreach (string file in files)
            {
                using (StreamReader reader = new StreamReader(file))
                {
                    string line = null;
                    while ((line = reader.ReadLine()) != null)
                    {
                        TradeFullinfoGetResponse rsp = TopUtils.ParseResponse<TradeFullinfoGetResponse>(line);
                        Trade trade = rsp.Trade;
                        Console.WriteLine("处理订单：" + trade.Tid);
                    }
                }
            }
        }

        /// <summary>
        /// 返回是否是大数据量
        /// </summary>
        /// <param name="sessionKey"></param>
        /// <returns></returns>
        public static bool isLargeData(string sessionKey, DateTime startTime, DateTime endTime)
        {
            var count = TradeAPI.GetTradesSoldGetResponseCount(sessionKey, startTime, endTime);

            if (count >= 1000)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 根据卖家Nick查询上一个同步的时间点
        /// </summary>
        /// <param name="sellerNick"></param>
        /// <returns></returns>
        public static DateTime GetLastSyncTime(string sellerNick)
        {
            return DateTime.Now;
        }
    }
}
